<template>
  <div>
    <div class="gva-search-box">
      <el-form ref="searchForm" :inline="true" :model="searchInfo">
        <el-form-item label="用户昵称">
          <el-input v-model="searchInfo.name" placeholder="用户昵称" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="searchInfo.phone" placeholder="手机号码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <el-table :data="tableData" row-key="id">
        <el-table-column align="left" label="用户ID" min-width="150" prop="id" />
        <el-table-column align="left" label="用户昵称" min-width="150" prop="name" />
        <el-table-column align="left" label="手机号码" min-width="150" prop="phone" />
        <el-table-column align="left" label="启用状态" min-width="150">
          <template #default="scope">
            <span>{{ displayStatus(scope.row.status) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="left" label="操作" min-width="150">
          <template #default="scope">
            <el-button class="edit" @click="fnChange(scope.row)">修改用户状态</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination
          :current-page="page"
          :page-size="page_size"
          :page-sizes="[10, 30, 50, 100]"
          :total="totals"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-drawer
      v-model="dialogFormVisible"
      size="30%"
      :before-close="closeDialog"
      :show-close="false"
    >
      <template #header>
        <span class="text-lg">用户状态</span>
        <div>
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="changeState">确 定</el-button>
        </div>
      </template>
      <el-form ref="platForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :value="1">正常</el-radio>
            <el-radio :value="2">禁用</el-radio>
            <el-radio :value="3">注销</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>
<script setup>
import { ref, onMounted, computed } from "vue";

import { userList, userStatus } from "@/api/platformUser";
import { ElMessage } from "element-plus";

let searchInfo = ref({
  name: "",
  phone: ""
});
let tableData = ref([]);
let totals = ref(0);
let page = ref(1);
let page_size = ref(10);
let dialogFormVisible = ref(false);
let form = ref({
  status: 1
});
const rules = ref({
  status: [{ required: true, message: "请选择用户状态", trigger: "blur" }]
});

onMounted(() => {
  getList();
});

const displayStatus = computed(() => {
  return val => {
    if (val === 1) {
      return "正常";
    } else if (val === 2) {
      return "禁用";
    } else if (val === 3) {
      return "注销";
    }
  };
});
const fnChange = row => {
  form.value = row;
  dialogFormVisible.value = true;
};
const closeDialog = () => {
  dialogFormVisible.value = false;
};
const changeState = row => {
  let params = {
    id: form.value.id,
    status: form.value.status
  };
  userStatus(params).then(response => {
    if (response.code === 0) {
      ElMessage({
        message: response.msg ? response.msg : "操作成功",
        type: "success"
      });
      closeDialog();
      page.value = 1;
      page_size.value = 10;
      getList();
    }
  });
};
const getList = () => {
  let params = {
    page: page.value,
    page_size: page_size.value,
    name: searchInfo.value.name,
    phone: searchInfo.value.phone ? Number(searchInfo.value.phone) : undefined
  };
  userList(params).then(response => {
    if (response.code === 0) {
      tableData.value = response.data.list || [];
      totals.value = response.data.total || 0;
    }
  });
};
const handleCurrentChange = val => {
  page.value = val;
  getList();
};
const handleSizeChange = val => {
  page_size.value = val;
  getList();
};
const onSubmit = () => {
  page.value = 1;
  page_size.value = 10;
  getList();
};
const onReset = () => {
  searchInfo.value = {
    name: "",
    phone: ""
  };
  page.value = 1;
  page_size.value = 10;
  totals.value = 0;
  getList();
};
</script>
<style>
</style>